'use strict';
const mongoose = require('mongoose');
const config = require('../config/config.unittest.js')({});
const fs = require('fs');
const readline = require('readline');

let ParserBetter = require('../app/utils/rss/parser');
let parserBetter = new ParserBetter({timeout: 10000});

const UserModel = require('../app/model/User')({
  mongoose,
});
const TopicModel = require('../app/model/Topic')({
  mongoose,
});

mongoose.connect(config.mongoose.url, config.mongoose.options, function (err) {
  if (err) {
    console.error('connect to %s error: ', config.mongoose, err.message);
    process.exit(1);
  }
});

async function main() {
  const default_boards_old = ['美食', '食物'];
  const default_boards_new = ['美食'];

  let r1 = await UserModel.updateMany(
    {default_boards: default_boards_old},
    {$set: {default_boards: default_boards_new}}
  );

  let r2 = await TopicModel.updateMany(
    {boards: default_boards_old},
    {$set: {boards: default_boards_new, tags: ['播客', ...default_boards_new]}}
  );

  console.log(r1);
  console.log(r2);


  process.exit(0);
}

main();


function getBoards(categories) {
  let boards = [...new Set(categories)].filter(v => v);
  boards = boards.map(v => v.replace('&amp;', '&'));
  return boards;
}

function getTags(categoriesWithSubs) {
  let tags = ['播客'];
  categoriesWithSubs.forEach(v => {
    tags.push(v.name);
    if (v.subs) {
      for (const sub of v.subs) {
        tags.push(sub.name);
      }
    }
  });
  tags = [...new Set(tags)].filter(v => v);
  tags = tags.map(v => v.replace('&amp;', '&'));
  return tags;
}


